--负责显示层的逻辑控制
import("..model.Context")
import(".StateBase")
import(".RushState")
import(".YaoShuiState")
import(".GameOverState")
ViewController =class("ViewController")
function ViewController:ctor()

end

function ViewController:restart()
   self:resetTime()
   self:setStateRunning()
end

function ViewController:isGameOver()
  return Context.state=="GAMEOVER"
end

function ViewController:isRunning()
  return Context.state=="RUNNING"
end

function ViewController:resetTime()
  Context.now=0
end
function ViewController:isRush()
  return Context.state=="RUSH"
end



function ViewController:onTiming()
   self:getStateHandler():onTiming()
end

function ViewController:onStateExit()
  self:getStateHandler():onExit()
end


function ViewController:getStateHandler()
  if self:isRush() then
    return rushStateHandler
  end
  if Context.state=="YAOSHUI" then
    return yaoshuiStateHandler
  end  
  if Context.state =="GAMEOVER" then
    return gameOverStateHandler
  end
  return defaultStateHandler
end
function ViewController:setStateRush()
  Context.state="RUSH"
   self:getStateHandler():onEnter()
end

function ViewController:setStateRunning()
  Context.state="RUNNING"
  self:getStateHandler():onEnter()
end
function ViewController:setStateGameOver()
  Context.state ="GAMEOVER"
  self:getStateHandler():onEnter()
end

function ViewController:onPillarEnter(ui)
   self:getStateHandler():onPillarEnter(ui)

end

function ViewController:onColliWithDown(ui)
    self:getStateHandler():onColliWithDown(ui)

end

function ViewController:onColliWithUp(ui)
self:getStateHandler():onColliWithUp(ui)
end

function ViewController:setStateYaoShui()
     Context.state="YAOSHUI"
    self:getStateHandler():onEnter()
end


function ViewController:tick(interval)
 Context.now=Context.now+interval
end
--当前相对时间
function ViewController:now()
 return Context.now
end

defaultStateHandler =StateBase.new()
rushStateHandler =RushState.new()
yaoshuiStateHandler =YaoShuiState.new()
gameOverStateHandler =GameOverState.new()
viewController =ViewController.new()